package com.snack.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.snack.shop.entity.MerchantMessage;
import com.snack.shop.mapper.MerchantMessageMapper;
import com.snack.shop.service.MerchantMessageService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

@Service
@RequiredArgsConstructor
@Slf4j
public class MerchantMessageServiceImpl implements MerchantMessageService {

    private final MerchantMessageMapper merchantMessageMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public MerchantMessage create(Long merchantId, Integer type, String title, String content, String bizType, Long bizId) {
        MerchantMessage msg = new MerchantMessage();
        msg.setMerchantId(merchantId);
        msg.setType(type);
        msg.setTitle(title);
        msg.setContent(content);
        msg.setBizType(bizType);
        msg.setBizId(bizId);
        msg.setReadStatus(0);
        merchantMessageMapper.insert(msg);
        log.info("创建商家消息：merchantId={}, type={}, title={}, bizType={}, bizId={}", merchantId, type, title, bizType, bizId);
        return msg;
    }

    @Override
    public Page<MerchantMessage> pageByMerchant(Long merchantId, Integer type, Integer current, Integer size) {
        Page<MerchantMessage> page = new Page<>(current == null ? 1 : current, size == null ? 10 : size);
        LambdaQueryWrapper<MerchantMessage> wrapper = new LambdaQueryWrapper<MerchantMessage>()
                .eq(MerchantMessage::getMerchantId, merchantId)
                .eq(type != null, MerchantMessage::getType, type)
                .orderByDesc(MerchantMessage::getCreateTime);
        return merchantMessageMapper.selectPage(page, wrapper);
    }

    @Override
    public Integer getUnreadCount(Long merchantId) {
        return Math.toIntExact(merchantMessageMapper.selectCount(new LambdaQueryWrapper<MerchantMessage>()
                .eq(MerchantMessage::getMerchantId, merchantId)
                .eq(MerchantMessage::getReadStatus, 0)));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void markRead(Long merchantId, Long messageId) {
        MerchantMessage db = merchantMessageMapper.selectById(messageId);
        if (db == null || !db.getMerchantId().equals(merchantId)) {
            return;
        }
        db.setReadStatus(1);
        db.setReadTime(LocalDateTime.now());
        merchantMessageMapper.updateById(db);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void markAllRead(Long merchantId) {
        MerchantMessage entity = new MerchantMessage();
        entity.setReadStatus(1);
        entity.setReadTime(LocalDateTime.now());
        merchantMessageMapper.update(entity, new LambdaUpdateWrapper<MerchantMessage>()
                .eq(MerchantMessage::getMerchantId, merchantId)
                .eq(MerchantMessage::getReadStatus, 0));
    }
}
